package com.origin.app.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.Matrix
import android.view.Gravity
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import com.amap.api.maps.model.BitmapDescriptor
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.origin.app.MyApplication
import com.origin.app.R
import com.origin.app.databinding.ToastLayoutBinding
import com.origin.uilibrary.SelectorFactory
import com.origin.uilibrary.xbanner.XBannerUtils
import com.origin.utils.AnnotationList
import com.origin.utils.ToastUtils
import com.origin.utils.screen.ScreenUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


object AppUtils {

    fun getLocationIcon(): BitmapDescriptor {
        val bitmapRes=R.mipmap.locate_arrow
        var bitmap = BitmapFactory
            .decodeResource(MyApplication.getInstance().resources,
                bitmapRes
            );
        val originalWidth = bitmap.width;
        val originalHeight = bitmap.height;
        val width = XBannerUtils.dp2px(MyApplication.getInstance(), 30f)
        val height = (width / (originalWidth / (originalHeight * 1.0f))).toInt()

        bitmap = resizeBitmap(bitmap, width, height)
        return  BitmapDescriptorFactory.fromBitmap(
            bitmap
        )
    }
    fun getPointInfoAvatar(state:Int,isCar:Boolean): BitmapDescriptor {
        val bitmapRes=  if(state==1){
            if(isCar) {R.mipmap.marker_car_normal}else{ R.mipmap.marker_normal}
        }else if(state==2){
            if(isCar) {R.mipmap.marker_car_abnormal}else{ R.mipmap.marker_abnormal}
        }else if (state==3){
            if(isCar) {R.mipmap.marker_car_offline}else{ R.mipmap.marker_offline}
        }else {
            if(isCar) {R.mipmap.marker_car_abnormal}else{ R.mipmap.marker_abnormal}
        }
        var bitmap = BitmapFactory
            .decodeResource(MyApplication.getInstance().resources,
                bitmapRes
            );
        val originalWidth = bitmap.width;
        val originalHeight = bitmap.height;
        val width = XBannerUtils.dp2px(MyApplication.getInstance(), 30f)
        val height = (width / (originalWidth / (originalHeight * 1.0f))).toInt()

        bitmap = resizeBitmap(bitmap, width, height)
      return  BitmapDescriptorFactory.fromBitmap(
            bitmap
        )
    }

    private var scopeUi = CoroutineScope(Dispatchers.Main)

    fun getStatusBarHeight():Int{
        return ScreenUtils.getStatusBarHeight(MyApplication.getInstance())
    }
    fun showLayoutToast(string: String) {
        scopeUi.launch {

            val stealthApplication = MyApplication.getInstance()
            val inflater = stealthApplication?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val layoutToastBinding = DataBindingUtil.inflate<ToastLayoutBinding>(inflater, R.layout.toast_layout, null, false)
            layoutToastBinding.root.background = null;
            layoutToastBinding.root.background = SelectorFactory.newShapeSelector().setDefaultBgColor(
                Color.parseColor("#80000000"))
                .setCornerRadius(SelectorFactory.dp2px(stealthApplication, 18.0f)).create()
            layoutToastBinding.msg.text = string
            ToastUtils.showLayout(
                stealthApplication, layoutToastBinding.root, AnnotationList.LENGTH_SHORT, ToastUtils.Location(
                    Gravity.CENTER, 0, 0)
            )
        }

    }
    fun alterSizeBitmap(bitmap: Bitmap, newWidth: Int, newHeight: Int): Bitmap {
        //计算压缩的比率

        val scaleWidth = (newWidth.toFloat()) / bitmap.width

        val scaleHeight = (newHeight.toFloat()) / bitmap.height

        //获取想要缩放的matrix
        val matrix: Matrix = Matrix()

        matrix.postScale(scaleWidth, scaleHeight)

        //获取新的bitmap
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
    }

    fun resizeBitmap(original: Bitmap, newWidth: Int, newHeight: Int): Bitmap {
        return Bitmap.createScaledBitmap(original, newWidth, newHeight, true)
    }


}